﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ChatApp.Core.Facade;
using ChatApp.Core.Info;
using ChatApp.Models;

namespace ChatApp.Controllers
{
    public class UserController : Controller
    {

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(User pUser)
        {
            if (ModelState.IsValid)
            {
                var aUserInfo = pUser.Authenticate(pUser.AUserInfo.UserName, pUser.AUserInfo.Password);
                if (aUserInfo != null)
                {
                    FormsAuthentication.SetAuthCookie(pUser.AUserInfo.UserName, false);
                    Session["UserInfo"] = aUserInfo;
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Login data is incorrect!");
                }
            }
            return View(pUser);
        }
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
    }
}
